//
//  CQAddViewController.m
//  HiChat
//
//  Created by wqiong on 16/1/15.
//  Copyright © 2016年 wqiong. All rights reserved.
//

#import "CQAddViewController.h"
#import "CQContactAddView.h"

@interface CQAddViewController () <CQAddViewDelegate, IChatManagerBuddy, UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *tableV;
@property (nonatomic, retain) NSMutableArray *newsArr;

@end

@implementation CQAddViewController

- (void)dealloc
{
    [_newsArr release];
    [_tableV release];
    self.tableV.delegate = nil;
    self.tableV.dataSource = nil;
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    [[EaseMob sharedInstance].chatManager addDelegate:self delegateQueue:nil];
    
    [self creatTabView];
    [self creataddView];
}


- (void)sendNameField:(NSString *)nameText
{
    
    EMChatCommand *cmdChat = [[EMChatCommand alloc] init];
    cmdChat.cmd = @"reason";
    EMCommandMessageBody *body = [[EMCommandMessageBody alloc] initWithChatObject:cmdChat];
    // 生成message
    EMMessage *message = [[EMMessage alloc] initWithReceiver:nameText bodies:@[body]];
    message.messageType = eMessageTypeChat; // 设置为单聊消息
    //message.messageType = eConversationTypeGroupChat;// 设置为群聊消息
    //message.messageType = eConversationTypeChatRoom;// 设置为聊天室消息

    
    
    EMError *error = nil;
    BOOL isSuccess = [[EaseMob sharedInstance].chatManager addBuddy:nameText message:@"我想加您为好友" error:&error];
    if (isSuccess && !error) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"通知" message:@"发送成功" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        [self presentViewController:alertC animated:YES completion:^{
            
            
        }];
    }
}
- (void)creataddView
{
    CQContactAddView *addV = [[CQContactAddView alloc] initWithFrame:CGRectMake(10, 0, self.view.bounds.size.width - 20, 60)];
    addV.nameAddDelegate = self;
    self.tableV.tableHeaderView = addV;
//    [self.view addSubview:addV];

}

- (void)creatTabView
{
    self.tableV = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    UIImageView *imageV = [[UIImageView alloc] initWithFrame:self.tableV.frame];
    imageV.image = [UIImage imageNamed:@"add"];
    self.tableV.backgroundView = imageV;
    self.tableV.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:self.tableV];
    [_tableV release];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.newsArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellAdd = @"cellAdd";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellAdd];
    
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:cellAdd] autorelease];
        cell.backgroundColor = [UIColor clearColor];
    }
    cell.textLabel.text = [self.newsArr objectAtIndex:indexPath.row];
    
    return cell;

}

/*!
 @method
 @brief 好友请求被接受时的回调
 @discussion
 @param username 之前发出的好友请求被用户username接受了
 */
- (void)didAcceptedByBuddy:(NSString *)username
{
    EMChatCommand *cmdChat = [[EMChatCommand alloc] init];
    cmdChat.cmd = @"reason";
    EMCommandMessageBody *body = [[EMCommandMessageBody alloc] initWithChatObject:cmdChat];
    // 生成message
    EMMessage *message = [[EMMessage alloc] initWithReceiver:username bodies:@[body]];
    message.messageType = eMessageTypeChat; // 设置为单聊消息
    //message.messageType = eConversationTypeGroupChat;// 设置为群聊消息
    //message.messageType = eConversationTypeChatRoom;// 设置为聊天室消息
    
    NSString *news = [NSString stringWithFormat:@"%@接受了您的请求",username];
    [self.newsArr addObject:news];
    [self.tableV reloadData];
    
}

/*!
 @method
 @brief 好友请求被拒绝时的回调
 @discussion
 @param username 之前发出的好友请求被用户username拒绝了
 */
- (void)didRejectedByBuddy:(NSString *)username
{
    EMChatCommand *cmdChat = [[EMChatCommand alloc] init];
    cmdChat.cmd = @"reason";
    EMCommandMessageBody *body = [[EMCommandMessageBody alloc] initWithChatObject:cmdChat];
    // 生成message
    EMMessage *message = [[EMMessage alloc] initWithReceiver:username bodies:@[body]];
    message.messageType = eMessageTypeChat; // 设置为单聊消息
    //message.messageType = eConversationTypeGroupChat;// 设置为群聊消息
    //message.messageType = eConversationTypeChatRoom;// 设置为聊天室消息
    
    NSString *news = [NSString stringWithFormat:@"%@拒绝了您的请求",username];
    [self.newsArr addObject:news];
    [self.tableV reloadData];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
